package com.c2b.algorithm.newcoder;

/**
 * <a href='https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024?tpId=295&tqId=23286&ru=%2Fexam%2Foj&qru=%2Fta%2Fformat-top101%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj'>删除有序链表中重复的元素-II </a>
 *
 * @author c2b
 * @since 2024/3/14 11:52
 */
public class BM16DeleteDuplicates {
    public ListNode deleteDuplicates (ListNode head) {
        if (head == null || head.next == head) {
            return head;
        }
        // 给链表加一个虚拟节点，避免链表头被删除
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode currNode = dummy;
        while (currNode.next != null && currNode.next.next != null) {
            if (currNode.next.val == currNode.next.next.val) {
                int val = currNode.next.val;
                while (currNode.next != null && currNode.next.val == val) {
                    currNode.next = currNode.next.next;
                }
            } else {
                currNode = currNode.next;
            }
        }
        return dummy.next;
    }
}
